#ifndef bnb_video_context_h
#define bnb_video_context_h

#include "SDL.h"
#include "singleton.h"

/**
  * Class in charge of initializing the video system (SDL).
  * It gives access to the current renderer and allows several
  * toplevel operations on it, like switching to fullscreen.
  *
  * This class should never be initialized directly but through
  * the main application controller.
  */
class BNBVideoContext : public Singleton<BNBVideoContext> {
  friend class Singleton<BNBVideoContext>;

  public:
    /**
      * Initializes an SDL video context (window with a renderer).
      * Beware : the SDL library must have been previously initialized
      * with SDL_INIT_VIDEO flag set.
      *
      * @p width initial width (may come from a configuration file)
      * @p height initial height (may come from a configuration file)
      * @p fullscreen run in fullscreen by default if true
      * @sa toggleFullscreen
      */
    void initialize (int width, int height, bool fullscreen);

    /**
      * Attempt to switch to fullscreen mode if @p state is true, else go back
      * to windowed mode.
      * Note : Switching to fullscreen may fail.
      * @return true if the state could be set, else false.
      */
    bool toggleFullscreen (bool state);

    /**
      * Returns the video renderer.
      */
    inline SDL_Renderer* getRenderer ();

  private:
    inline BNBVideoContext ();
    /**
      * Destroys the SDL video context.
      */
    inline ~BNBVideoContext ();
    /**
      * Will loop into all the available renderers until it can initialize an
      * hardware accel one. Else, do the same but without hardware accel.
      */
    void initRenderer ();

  private:
    SDL_Window* window;
    SDL_Renderer* renderer;
};

SDL_Renderer* BNBVideoContext::
getRenderer () {
  return renderer;
}

BNBVideoContext::
BNBVideoContext () : window(NULL), renderer(NULL) {
  // Nothing to do.
}

BNBVideoContext::
~BNBVideoContext () {
  if (renderer != NULL) {
    SDL_DestroyRenderer(renderer);
  }
  if (window != NULL) {
    SDL_DestroyWindow(window);
  }
}

#endif

